<?php
/**
*
* @package phpBB3 stockvirtual
* @version $Id$
* @copyright (c) 2005 phpBB Group, 2011 Minway Hsu
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/

// get facebook app id.
function get_fb_app_id()
{	
	// To test in localhost(WinXP), add this for convenience.
	if (PHP_OS == 'WINNT')
		return '138584166173529';
		
	return '144571395591718';
}

function get_fb_app_secret()
{	
	// To test in localhost(WinXP), add this for convenience.
	if (PHP_OS == 'WINNT')
		return '0f092e4e1eb18706399b701a8a820ae1';
		
	return '0df957a416eb157f7d583ba36c11d618';
}

function get_fb_app_url()
{	
	// To test in localhost(WinXP), add this for convenience.
	if (PHP_OS == 'WINNT')
		return 'http://apps.facebook.com/minwaytest/';
		
	return 'http://apps.facebook.com/stockvirtual/';
}

// login FB users and create corresponding accounts. 
function login_fb_user($fb_id, $name, $first_name, $last_name, $mail, $link, $gender, $timezone, $locale, $verified)
{
	global $db, $user;
	
	// get the user name from FB id. 
  	$sql 	= 'SELECT * FROM phpbb_facebook_user WHERE fb_id=' . $fb_id;
	$result	= $db->sql_query_limit($sql, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		// if user exist, login.
	  	$user_id = $row['user_id'];  			
  		$user->session_create($user_id, false, true);
	}
  	else
  	{
		// or create the user and insert FB info.
	  	$username = $name;			
		$password = phpbb_hash(' ' . $fb_id . time());
			
		// try to create a unique name.
		if (username_exist($username))
		{
			$username = $username . '(Facebook)';				
			if (username_exist($username))
			{
				$username = $username . '(' . $local . ')';
				if (username_exist($username))
				{
					$username = $username . '(' . $gender . ')';
					if (username_exist($username))
					{
						$username = $username . '(' . $fb_id . ')';						
					}
				}
			}
		}
			
		if ($mail == '')
			$mail = "$fb_id@facebook.com";
		if( $link == '')
			$link = 'http://www.facebook.com/profile.php?id=' .  $fb_id;
			
	  	// Setup User Data
		$user_row = array(
		    	'username' => $username,
			    'user_password' => phpbb_hash($password),
			    'user_email' => $mail,
			    'group_id' => 2,
		    	'user_timezone' => '8',
			    'user_dst' => '0',
			    'user_lang' => $config['default_lang'],
			    'user_type' => '0',
		    	'user_actkey' => '',
			    'user_ip' => $_SERVER['REMOTE_ADDR'],
			    'user_inactive_reason' => '0',
			    'user_website' => $link,
		    	'user_inactive_time' => '0');
		    	
			$user_id = user_add($user_row);
		
			if ($user_id == '')
			{
				// if fail to add user, return.
				return false;
			}
				
			$user->session_create($user_id, false, true);
			
			$sql = 'INSERT INTO phpbb_facebook_user (fb_id, user_id, name, first_name, last_name, link, gender, timezone, locale, verified) VALUES (';
			$sql .= ' ' . $fb_id . ', ' . $user_id . ', "' . $name . '", "' . $first_name . '", "' . $last_name . '", "' . $link . '", "' . $gender . '", "' . $timezone . '", "' . $locale . '", "' . $verified . '")';
			$db->sql_query($sql);
		}	
}

// Check if the user is from facebook.
function get_user_fb_id($user_id)
{
	global $db;
	
	$user_id = (int)($user_id);
	
	$sql 	= 'SELECT * FROM phpbb_facebook_user WHERE user_id=' . $user_id;
	$result	= $db->sql_query_limit($sql, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		return $row['fb_id'];
	}
	
	return 0;
}

// Get the FB user email.
function get_user_fb_email($user_id)
{
	global $db;
	
	$user_id = (int)($user_id);
	
	$sql 	= 'SELECT * FROM phpbb_facebook_user WHERE user_id=' . $user_id;
	$result	= $db->sql_query_limit($sql, 1);
	if ($row = $db->sql_fetchrow($result))
	{
		return $row['email'];
	}
	
	return '';
}

// Check if the user has one or more fb friends registering in our app.
function has_fb_friends_registered($user_id)
{
	global $db;
	
	$user_id = (int)($user_id);
	
	$sql 	= 'SELECT * FROM sv_fb_friends WHERE user_id=' . $user_id ;
	$result	= $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{
		$fb_friend_id = $row['fb_friend_id'];
		
		$sql2 	= 'SELECT * FROM phpbb_facebook_user WHERE fb_id=' . $fb_friend_id ;
		$result2	= $db->sql_query($sql2);
		if ($db->sql_fetchrow($result2))
		{
			return true;
		}
	}
	
	return false;
}

// update FB friends relationship.
function update_fb_friends($user_id, $user_fb_id, $code)
{
	global $db;
	
	$app_id = get_fb_app_id();
	$app_url = get_fb_app_url();
	$app_secret = get_fb_app_secret();
		
	$url = 'https://graph.facebook.com/oauth/access_token?client_id='. $app_id . '&redirect_uri=' . urlencode($app_url) . '&client_secret=' . $app_secret . '&code=' . $code ;
	$ans = file_get_contents($url);
	
	if ($ans == false)
		return;
		
    $graph_url = "https://graph.facebook.com/me/friends?" . $ans;
    $friend_info = json_decode(file_get_contents($graph_url));
    
    foreach ($friend_info->data as $friend)
    {		
		$fb_friend_id =  $friend->id;
		$fb_friend_name = $friend->name;
		
		$sql 	= 'SELECT * FROM sv_fb_friends WHERE user_id=' . $user_id . ' AND user_fb_id=' . $user_fb_id . ' AND fb_friend_id=' . $fb_friend_id ;
		$result	= $db->sql_query_limit($sql, 1);
		if (!($db->sql_fetchrow($result)))
		{
			$sql = 'INSERT IGNORE INTO sv_fb_friends SET user_id=' . $user_id . ', user_fb_id=' . $user_fb_id . ', fb_friend_id=' . $fb_friend_id . ', fb_friend_name="' . $fb_friend_name . '"';	
			$result	= $db->sql_query($sql);
		}
    }	
}

// get facebook friends
function get_fb_friends($user_id)
{
	global $db;
	
	$fb_friends 	= array();
	$fb_friends[] 	= $user_id;
	$user_fb_id 	= get_user_fb_id($user_id);
	
	$sql	= "	SELECT user_id FROM phpbb_facebook_user WHERE fb_id in (SELECT user_fb_id FROM sv_fb_friends WHERE fb_friend_id = $user_fb_id) OR fb_id in (SELECT fb_friend_id FROM sv_fb_friends WHERE user_id = $user_id)";
	$result = $db->sql_query($sql); 
	while ($row = $db->sql_fetchrow($result))
	{
		$fb_friends[] = $row['user_id'];
	}
	
	return $fb_friends;
}

?>
